/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.core.service.impl;

import hu.glassy.core.model.Account;
import hu.glassy.core.model.ActivationToken;
import hu.glassy.core.service.EmailService;
import hu.glassy.core.util.mail.MimeMessagePreparatorImpl;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.apache.wicket.model.ResourceModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 * {@link EmailService} implementációja
 *
 * @author molnar.alexander@sonrisa.hu
 */
@Service
// @Transactional(readOnly = true)
public class EmailServiceImpl implements EmailService
{

  // --------------------------------------------------
  // ~ Constants
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Dependencies
  // --------------------------------------------------
  @Autowired
  private VelocityEngine velocityEngine;

  @Autowired
  private JavaMailSender mailSender;

  // --------------------------------------------------
  // ~ Private members
  // --------------------------------------------------
  @Value("${mail.encoding}")
  private String encoding;

  @Value("${email.fromaddress}")
  private String fromEmailAddress;

  @Value("${activation.email.template}")
  private String activationMessageTemplate;

  @Value("${password.reset.email.template}")
  private String passwordResetMessageTemplate;

  // --------------------------------------------------
  // ~ Service Method implementation
  // --------------------------------------------------
  public void sendActivationEmail(final Account account, final ActivationToken token)
  {
    final String title = new ResourceModel("activation.email.subject").getObject();
    final MimeMessagePreparatorImpl preparator = new MimeMessagePreparatorImpl();
    preparator.setEncoding(encoding);
    preparator.setFromEmailAddress(fromEmailAddress);
    preparator.setSubject(title);
    preparator.setTargetEmailAddress(account.getEmail());

    final Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("title", title);
    templateParams.put("name", account.getName());
    templateParams.put("token", token.getToken());
    // TODO: Localize email templates
    final String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, activationMessageTemplate, encoding, templateParams);
    preparator.setText(text);

    mailSender.send(preparator);
  }

  public void sendPasswordResetEmail(final Account account, final String password)
  {
    final String title = new ResourceModel("passwordreset.email.subject").getObject();
    final MimeMessagePreparatorImpl preparator = new MimeMessagePreparatorImpl();
    preparator.setEncoding(encoding);
    preparator.setFromEmailAddress(fromEmailAddress);
    preparator.setSubject(title);
    preparator.setTargetEmailAddress(account.getEmail());

    final Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("title", title);
    templateParams.put("name", account.getName());
    templateParams.put("password", password);
    // TODO: Localize email templates
    final String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, passwordResetMessageTemplate, encoding, templateParams);
    preparator.setText(text);

    mailSender.send(preparator);
  }
}
